package com.youthburin.rmp.services.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.youthburin.base.services.AbstractService;
import com.youthburin.base.springjpa.MyRepository;
import com.youthburin.rmp.domain.RmpDict;
import com.youthburin.rmp.domain.RmpDictPK;
import com.youthburin.rmp.domain.RmpDictType;
import com.youthburin.rmp.repositories.RmpDictRepository;
import com.youthburin.rmp.repositories.RmpDictTypeRepository;
import com.youthburin.rmp.services.RmpDictTypeService;

@Service
public class RmpDictTypeServiceImpl extends AbstractService<RmpDictType, String> implements RmpDictTypeService {
	@Autowired
	private RmpDictTypeRepository rmpDictTypeRepository;
	@Autowired
	private RmpDictRepository rmpDictRepository;

	@Override
	public MyRepository<RmpDictType, String> getMyRepository() {
		return rmpDictTypeRepository;
	}

	@Override
	public RmpDictType save(RmpDictType rmpDictType) {
		RmpDictType newRmpDictType = rmpDictTypeRepository.save(rmpDictType);
		if (rmpDictType.getRmpDicts() != null && !rmpDictType.getRmpDicts().isEmpty()) {
			for (RmpDict dict : rmpDictType.getRmpDicts()) {
				RmpDictPK id = new RmpDictPK();
				id.setTypeCode(rmpDictType.getCode());
				id.setCode(dict.getCode());
				dict.setId(id);

				rmpDictRepository.save(dict);
			}
		}
		return newRmpDictType;

	}

	@Override
	public RmpDictType getIgnoreCase(String code) {
		return rmpDictTypeRepository.getIgnoreCase(code);
	}

}
